/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.client.reports;

import com.google.gwt.user.client.ui.*;
import nfse.gui.util.NetUtil;

public class StatsBarGraph extends FlexTable {

    public StatsBarGraph(StatsResult stats) {

        // int max = (int) (stats.maxValue * 1.1 / 10 + 1) * 10;

        int max = (int) (stats.maxValue * 1.1);
        int mult = 10;
        int cnt = 1;
        while (mult * 10 < max) {

            cnt++;
            if (cnt % 2 == 0) {
                mult = mult * 5;
            } else {
                mult = mult * 2;
            }
        }
        cnt = 1;
        while (cnt * mult < max) {
            cnt++;
        }
        max = mult * cnt;
        // max = mult*10;

        /*
         * int incr = 1; while (stats.maxValue > Math.pow(10, incr)) incr++;
         * long max = (long) Math.pow(10, incr); if (incr > 1) { long prevMax =
         * (long) Math.pow(10, incr - 1); while (max > stats.maxValue) max -=
         * prevMax; max += prevMax*2; }
         */
        int numStats = stats.stats.length;
        int colWidth = (396) / numStats;
        int width = (int) (396 * 0.95) / (numStats * 2);
        if (stats.direction == 0)
            width = width * 2;
        if (width > 75) {
            width = 75;
        }

        String tsStr = "";
        if (stats.minTS.equals(stats.maxTS))
            tsStr = stats.minTS.substring(0, stats.minTS.indexOf(" "));
        else
            tsStr = stats.minTS + " to " + stats.maxTS;

        String html = "<table id=\"q-graph\">" + "<caption></caption>" + "<thead>"

        // + "<tr>" + "<th></th>" + "<th class=\"sent\">Incoming</th>" + "<th
                // class=\"paid\">Outgoing</th>" + "</tr>"
                + "<tr>" + "<th></th>" + "</tr>" + "</thead>" + "<tbody>";

        // String headers = "";
        for (int i = 0; i < stats.stats.length; i++) {
            // System.out.println(stats.stats[i]);
            String style = "border-right: 1px dotted #C4C4C4; ";
            if (i == stats.stats.length - 1)
                style = " border-right: none;";

            html += "<tr class=\"qtr\" style=\"\">" + "<th scope=\"row\" style=\"" + style + " halign: center; left: "
                    + colWidth * i + "px;width: " + colWidth + "px;\">" + stats.stats[i].key + "</th>";

            // System.out.println(stats.direction);

            if (stats.direction == 0) {
                int height = (int) (((double) Integer.parseInt(stats.stats[i].incomingValue) / (double) max) * 300);
                if (height > 10) {
                    html += "<td class=\"sent bar\" style=\"" + style + " left: " + (colWidth * i) + "px; height: "
                            + height + "px; width: " + width + "px;\"><p><a title=\""
                            + stats.stats[i].incomingValueDisplay + "\">" + stats.stats[i].incomingValueDisplay
                            + "</a></p></td>";
                } else {
                    html += "<td class=\"sent bar\" style=\"" + style + " left: " + (colWidth * i)
                            + "px; height: 10px; width: " + width + "px;\"><a title=\""
                            + stats.stats[i].incomingValueDisplay + "\">" + stats.stats[i].incomingValueDisplay
                            + "</a></td>";
                }
            } else if (stats.direction == 1) {
                int height = (int) (((double) Integer.parseInt(stats.stats[i].incomingValue) / (double) max) * 300);
                if (height > 10) {
                    html += "<td class=\"sent bar\" style=\"" + style + " left: " + (colWidth * i) + "px; height: "
                            + height + "px; width: " + width + "px;\"><p><a title=\""
                            + stats.stats[i].incomingValueDisplay + "\">" + stats.stats[i].incomingValueDisplay
                            + "</a></p></td>";
                } else {
                    html += "<td class=\"sent bar\" style=\"" + style + " left: " + (colWidth * i)
                            + "px; height: 10px; width: " + width + "px;\"><a title=\""
                            + stats.stats[i].incomingValueDisplay + "\">" + stats.stats[i].incomingValueDisplay
                            + "</a></td>";
                }
                height = (int) (((double) Integer.parseInt(stats.stats[i].outgoingValue) / (double) max) * 300);
                if (height > 10) {
                    html += "<td class=\"paid bar\" style=\"" + style + " left: " + (colWidth * i + width)
                            + "px; height: " + height + "px; width: " + width + "px;\"><p><a title=\""
                            + stats.stats[i].outgoingValueDisplay + "\">" + stats.stats[i].outgoingValueDisplay
                            + "</a></p></td>";
                } else {
                    html += "<td class=\"paid bar\" style=\"" + style + " left: " + (colWidth * i + width)
                            + "px; height: 10px; width: " + width + "px;\"><a title=\""
                            + stats.stats[i].outgoingValueDisplay + "\">" + stats.stats[i].outgoingValueDisplay
                            + "</a></td>";
                }
            } else {
                int height = (int) (((double) Integer.parseInt(stats.stats[i].outgoingValue) / (double) max) * 300);
                if (height > 10) {
                    html += "<td class=\"paid bar\" style=\"" + style + " left: " + (colWidth * i + width)
                            + "px; height: " + height + "px; width: " + width + "px;\"><p><a title=\""
                            + stats.stats[i].outgoingValueDisplay + "\">" + stats.stats[i].outgoingValueDisplay
                            + "</a></p></td>";
                } else {
                    html += "<td class=\"paid bar\" style=\"" + style + " left: " + (colWidth * i + width)
                            + "px; height: 10px; width: " + width + "px;\"><a title=\""
                            + stats.stats[i].outgoingValueDisplay + "\">" + stats.stats[i].outgoingValueDisplay
                            + "</a></td>";
                }
                height = (int) (((double) Integer.parseInt(stats.stats[i].incomingValue) / (double) max) * 300);
                if (height > 10) {
                    html += "<td class=\"sent bar\" style=\"" + style + " left: " + (colWidth * i) + "px; height: "
                            + height + "px; width: " + width + "px;\"><p><a title=\""
                            + stats.stats[i].incomingValueDisplay + "\">" + stats.stats[i].incomingValueDisplay
                            + "</a></p></td>";
                } else {
                    html += "<td class=\"sent bar\" style=\"" + style + " left: " + (colWidth * i)
                            + "px; height: 10px; width: " + width + "px;\"><a title=\""
                            + stats.stats[i].incomingValueDisplay + "\">" + stats.stats[i].incomingValueDisplay
                            + "</a></td>";
                }

            }

            // html += "<td class=\"paid bar\" style=\"height:
            // 50px;\"><p>10</p></td>" + "</tr>";
            html += "</tr>";
        }
        // html += headers;
        html +=

        "</tbody>" + "</table>" +

        "<div id=\"ticks\">" + "<div class=\"tick\" style=\"height: 29px;\"><p>" + max + "</p></div>" +

        "<div class=\"tick\" style=\"height: 29px;\"><p>" + (int) (max * .9) + "</p></div>"
                + "<div class=\"tick\" style=\"height: 29px;\"><p>" + (int) (max * .8) + "</p></div>"
                + "<div class=\"tick\" style=\"height: 29px;\"><p>" + (int) (max * .7) + "</p></div>"
                + "<div class=\"tick\" style=\"height: 29px;\"><p>" + (int) (max * .6) + "</p></div>"
                + "<div class=\"tick\" style=\"height: 29px;\"><p>" + (int) (max * .5) + "</p></div>"
                + "<div class=\"tick\" style=\"height: 29px;\"><p>" + (int) (max * .4) + "</p></div>"
                + "<div class=\"tick\" style=\"height: 29px;\"><p>" + (int) (max * .3) + "</p></div>"
                + "<div class=\"tick\" style=\"height: 29px;\"><p>" + (int) (max * .2) + "</p></div>"

                + "<div class=\"tick\" style=\"height: 29px;\"><p>" + (int) (max * .1) + "</p></div>" + "</div>";

        HTMLPanel htmlPanel = new HTMLPanel(html);

        this.setHTML(0, 0, "<b>" + stats.title + "</b>");
        this.getFlexCellFormatter().addStyleName(0, 0, "font: 9px Helvetica, Geneva, sans-serif;");
        this.getFlexCellFormatter().addStyleName(0, 0, "tdcenter");
        this.setHTML(1, 0, "<i>" + tsStr + "</i>");
        this.getFlexCellFormatter().addStyleName(1, 0, "font: 9px Helvetica, Geneva, sans-serif;");
        this.getFlexCellFormatter().addStyleName(1, 0, "tdcenter");
        this.setWidget(2, 0, htmlPanel);

    }

}
